package com.skypan.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.skypan.myapplication.bean.EventR;
import com.skypan.myapplication.bean.LoginR;
import com.skypan.myapplication.bean.RangeR;
import com.skypan.myapplication.http.EventCallback;
import com.skypan.myapplication.http.RangeCallback;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;

import okhttp3.Call;
//个人追溯记录


public class MyReadActivity extends AppCompatActivity {
    private String url = "http://39.105.165.31:8080/event/query";
    LoginR localuser = LocalDataApplication.getInstance().getLocalResponse();
    String userId = localuser.data.userId;
    private MyReadActivity.MyAdapter adapter;
    private List<EventR.ListBean> list ;
    private ListView myreadnote;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_myread);
        myreadnote = findViewById(R.id.my_read_note);

        eventquery(userId);
    }


    public void eventquery(String userId) {
        OkHttpUtils
                .get()
                .url(url)//
                .addParams("userId", userId)
                .build()//
                .execute(new EventCallback()
                {
                    @Override
                    public void onError(Call call, Exception e, int id)
                    {
                        Toast.makeText(MyReadActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(EventR response, int id)
                    {
//                        if(response.getCode()==0){
                        list = response.data.list;
                        adapter = new MyReadActivity.MyAdapter(MyReadActivity.this,list);
                        myreadnote.setAdapter(adapter);
//                        Toast.makeText(getContext(),list.get(0).tagId,Toast.LENGTH_SHORT).show();

//                        }

                    }
                });
    }

    public class MyAdapter extends BaseAdapter {

        private Context context;
        private  List<EventR.ListBean> data;

        public MyAdapter(Context context, List<EventR.ListBean> data){
            this.context = context;
            this.data = data;

        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder = new ViewHolder();

            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.event_list,parent,false);
                viewHolder.item_event_eventId = (TextView) convertView.findViewById(R.id.item_event_eventId);
                viewHolder.item_event_tagId = (TextView) convertView.findViewById(R.id.item_event_tagId);
                viewHolder.item_event_userId = (TextView) convertView.findViewById(R.id.item_event_userId);
                viewHolder.item_event_eventDate = (TextView) convertView.findViewById(R.id.item_event_eventDate);
                viewHolder.item_event_location = (TextView) convertView.findViewById(R.id.item_event_location);
                viewHolder.item_event_eventStatus = (ImageView)convertView.findViewById(R.id.item_event_eventStatus);

                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.item_event_eventId.setText("事件编号："+data.get(position).getEventId());
            viewHolder.item_event_tagId.setText("标签编号："+data.get(position).getTagId());
            viewHolder.item_event_userId .setText("用户ID："+data.get(position).getUserId());
            viewHolder.item_event_eventDate.setText(data.get(position).getEventDate());
            viewHolder.item_event_location.setText("追溯地点："+data.get(position).getEventLocation());
            if(data.get(position).getEventStatus().equals("0"))
//            viewHolder.item_event_eventStatus.setText("追溯结果："+"成功");
            viewHolder.item_event_eventStatus.setImageResource(R.drawable.status_success);
            else viewHolder.item_event_eventStatus.setImageResource(R.drawable.status_failed);
//                viewHolder.item_event_eventStatus.setText("追溯结果："+"失败");

            return convertView;
        }


        class ViewHolder {
            public TextView item_event_eventId;
            public TextView item_event_tagId;
            public TextView item_event_userId;
            public TextView item_event_eventDate;
            public TextView item_event_location;
            public ImageView item_event_eventStatus;
        }


    }

}
